package com.atxuwei.yygh.hosp.controller.api;

import com.atxuwei.yygh.common.exception.YyghException;
import com.atxuwei.yygh.common.helper.HttpRequestHelper;
import com.atxuwei.yygh.common.result.Result;
import com.atxuwei.yygh.common.result.ResultCodeEnum;
import com.atxuwei.yygh.common.utils.MD5;
import com.atxuwei.yygh.hosp.service.DepartmentService;
import com.atxuwei.yygh.hosp.service.HospitalService;
import com.atxuwei.yygh.hosp.service.HospitalSetService;
import com.atxuwei.yygh.hosp.service.ScheduleService;
import com.atxuwei.yygh.model.hosp.Department;
import com.atxuwei.yygh.model.hosp.Hospital;
import com.atxuwei.yygh.model.hosp.Schedule;
import com.atxuwei.yygh.vo.hosp.DepartmentQueryVo;
import com.atxuwei.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    //删除排班接口
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object>  paramMap = HttpRequestHelper.switchMap(requestMap);
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

    //查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object>  paramMap = HttpRequestHelper.switchMap(requestMap);
        String  hosCode=(String)paramMap.get("hoscode");
        String  depcode=(String)paramMap.get("depcode");
        int page=StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String)paramMap.get("page"));
        int limit=StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String)paramMap.get("limit"));
        //校验签名key是否一致，省略
        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hosCode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel=scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object>  paramMap = HttpRequestHelper.switchMap(requestMap);
        //校验签名key是否一致，省略
        String hoscode = (String)paramMap.get("hoscode");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object>  paramMap = HttpRequestHelper.switchMap(requestMap);
        String  hosCode=(String)paramMap.get("hoscode");
        String  depcode=(String)paramMap.get("depcode");
        //校验签名key是否一致，省略
        departmentService.remove(hosCode,depcode);
        return Result.ok();
    }

    //查询科室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object>  paramMap = HttpRequestHelper.switchMap(requestMap);
        String  hosCode=(String)paramMap.get("hoscode");
        int page=StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String)paramMap.get("page"));
        int limit=StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String)paramMap.get("limit"));
        //校验签名key是否一致，省略
        DepartmentQueryVo  departmentQueryVo=new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hosCode);
        Page<Department> pageModel=departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }

    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object>  paramMap = HttpRequestHelper.switchMap(requestMap);
        //校验签名key是否一致，省略
        //调用service方法
        departmentService.save(paramMap);
        return Result.ok();
    }

    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object>  paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hosCode =(String)paramMap.get("hoscode");
        //获取医院传过来的签名
        String hospSign =(String)paramMap.get("sign");
        String signKey=hospitalSetService.getSignKey(hosCode);
        //加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断是否一致
        if(!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法实现根据医院编号查询
        Hospital hospital=hospitalService.getByHoscode(hosCode);
        return Result.ok(hospital);
    }

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object>  paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院传过来的签名
        String hospSign =(String)paramMap.get("sign");
        //根据传过来的医院编码，查询数据库，查询签名
        String hosCode =(String)paramMap.get("hoscode");
        String signKey=hospitalSetService.getSignKey(hosCode);
        //加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断是否一致
        if(!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中‘+’转换为‘’，因此需要转换回来
        String logoData =(String) paramMap.get("logoData");
        logoData=logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);
        hospitalService.save(paramMap);
        return Result.ok();
    }

}
